package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.ThinkTankService;
import com.yuwubao.uav.tool.util.ArticleSearchService;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 专业智库管理
 */
@RestController
@RequestMapping("/bk")
public class ThinkTankController {

    private static final Logger logger = LogManager.getLogger(ThinkTankController.class);
    
    @Resource
    private ThinkTankService thinkTankService;

    @Resource
    private ArticleSearchService articleSearchService;

    @LoginRequired
    @PostMapping("/addThinkTank")
    @ApiOperation(value = "新增专业智库")
    public JsonResult addThinkTank(
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "专业智库标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "专业智库副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "简介") @RequestParam(value = "description",required = false) String description,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("imgUrl", imgUrl);
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("img", imgUrl);
            pageData.put("description", description);

            thinkTankService.addThinkTank(pageData);

            try {
                pageData.put("tableName","t_thinktank");
                articleSearchService.createDoc(pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateThinkTank")
    @ApiOperation(value = "更新专业智库")
    public JsonResult updateThinkTank(
            @ApiParam(value = "图片url") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "专业智库ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "专业智库标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "专业智库副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "简介") @RequestParam(value = "description",required = false) String description,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("imgUrl", imgUrl);
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("img", imgUrl);
            pageData.put("description", description);

            thinkTankService.updateThinkTank(pageData);

            try {
                pageData.put("tableName","t_thinktank");
                articleSearchService.update(id,pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteThinkTank")
    @ApiOperation(value = "删除专业智库")
    public JsonResult deleteThinkTank(
            @ApiParam(value = "专业智库ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            thinkTankService.deleteThinkTank(pageData);

            try {
                if (StringUtil.isNotNvlOrBlank(ids)) {
                    pageData.put("tableName","t_thinktank");
                    for (String id : ids.split(",")) {
                        pageData.put("id",id);
                        articleSearchService.delete(pageData);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableThinkTank")
    @ApiOperation(value = "禁用或启用专业智库")
    public JsonResult disableOrEnableThinkTank(
            @ApiParam(value = "专业智库ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            thinkTankService.disableOrEnableThinkTank(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listThinkTank")
    @ApiOperation(value = "获取专业智库列表")
    public JsonResult listThinkTank(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(thinkTankService.listThinkTank(pageData));
            result.setResultNum(thinkTankService.listThinkTankCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addThinkTankType")
    @ApiOperation(value = "新增专业智库类型")
    public JsonResult addThinkTankType(
            @ApiParam(value = "专业智库类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "专业智库类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);
            pageData.put("remark", remark);

            thinkTankService.addThinkTankType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateThinkTankType")
    @ApiOperation(value = "更新专业智库类型")
    public JsonResult updateThinkTankType(
            @ApiParam(value = "专业智库类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "专业智库类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "专业智库类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("remark", remark);

            thinkTankService.updateThinkTankType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteThinkTankType")
    @ApiOperation(value = "删除专业智库类型")
    public JsonResult deleteThinkTankType(
            @ApiParam(value = "专业智库类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            thinkTankService.deleteThinkTankType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableThinkTankType")
    @ApiOperation(value = "禁用或启用专家")
    public JsonResult disableOrEnableThinkTankType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            thinkTankService.disableOrEnableThinkTankType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listThinkTankType")
    @ApiOperation(value = "获取专业智库类型列表")
    public JsonResult listThinkTankType(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(thinkTankService.listThinkTankType(pageData));
            result.setResultNum(thinkTankService.listThinkTankTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listThinkTankTypeAll")
    @ApiOperation(value = "获取专业智库类型列表所有")
    public JsonResult listThinkTankTypeAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(thinkTankService.listThinkTankTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
